package bimvu.lab1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

    private static final int SHOW_SUB_ACTIVITY_ONE = 1;
    private static final int SHOW_SUB_ACTIVITY_TWO = 2;

    private EditText firstNameField;
    private EditText lastNameField;
    private EditText ageField;
    private EditText phoneField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstNameField = (EditText) findViewById(R.id.firstNameField);
        lastNameField = (EditText) findViewById(R.id.lastNameField);
        ageField = (EditText) findViewById(R.id.ageField);
        phoneField = (EditText) findViewById(R.id.phoneField);
    }

    public void commandListener(View target)
    {
        switch (target.getId()) {
            case R.id.button_goForward:
                Intent wayToFarFarAway = new Intent(this, SubActivity.class);
                Person bravePerson = new Person();
                bravePerson.setFirstName(firstNameField.getText().toString());
                bravePerson.setLastName(lastNameField.getText().toString());
                String ageString = ageField.getText().toString();
                bravePerson.setAge(Integer.parseInt(ageString));
                bravePerson.setPhone(phoneField.getText().toString());
                Log.d("keep an eye open on this", getString(R.string.the_person_is_ready_to_go_) + bravePerson.toString());
                wayToFarFarAway.putExtra("Item1", bravePerson);
                startActivityForResult(wayToFarFarAway, SHOW_SUB_ACTIVITY_ONE);
                break;
            case R.id.button_secondActivity:
                Intent intent = new Intent(this, SecondActivity.class);
                startActivityForResult(intent, SHOW_SUB_ACTIVITY_TWO);
                break;
            default:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        updateTransitionsCounter();
    }

    private void updateTransitionsCounter() {
        TransitionsCounter counter = TransitionsCounter.getInstance();
        counter.increment();
        View counterLayout = findViewById(R.id.include1);
        View counterView = counterLayout.findViewById(R.id.transitionsCount);
        ((TextView) counterView).setText(counter.getValue().toString());
    }

}
